﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AirManager.Entities;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Transform;
using Configuration = NHibernate.Cfg.Configuration;


namespace AirManager
{
    //Saiu porque foi colocado o Repositoy Base como classe abstract
    //public class RepositoryBase<T> : RepositoryBase where T : class
    //{
    //}

    public abstract class RepositoryBase<T> where T : class
    {
        #region Configuration Methods

        private static ISessionFactory CreateSessionFactory()
        {
            const string sqlserverExpress = @"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=True";
            var dbConfig =
            MsSqlConfiguration.MsSql2005
                .ConnectionString(c =>
                                  c.Is(sqlserverExpress))
                .ShowSql();

            var configuration = Fluently
                .Configure()
                .Database(dbConfig)
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Fix>())
                .ExposeConfiguration(c => new SchemaExport(c).Create(false, false));
            
            return configuration.BuildSessionFactory();
        }

        private static ISession _currentSession;

        protected static ISession CurrentSession
        {
            get { return _currentSession ?? (_currentSession = CreateSessionFactory().OpenSession()); }
        }

        private static void BuildSchema(Configuration config)
        {
            new SchemaExport(config).Create(false, true);
        }

        private static void BuildSchemaNotRecreate(Configuration config)
        {
            new SchemaExport(config).Create(false, false);
        }

        #endregion

        #region CRUD Methods

        public T Get(object key)
        {
            //var mySession = CreateSessionFactory().OpenSession();
            //return mySession.Get<T>(key);
            return CurrentSession.Get<T>(key);
        }

        public void Save(T obj)
        {
            //var mySession = CreateSessionFactory().OpenSession();
            //mySession.SaveOrUpdate(obj);
            //mySession.Flush();
            CurrentSession.SaveOrUpdate(obj);
            CurrentSession.Flush();
        }

        public void Update(T obj)
        {
            //var mySession = CreateSessionFactory().OpenSession();
            CurrentSession.SaveOrUpdate(obj);
            CurrentSession.Flush();
        }

        public void Delete(T obj)
        {
            //var mySession = CreateSessionFactory().OpenSession();
            CurrentSession.Delete(obj);
            CurrentSession.Flush();
        }

        public IEnumerable<T> FindAll()
        {
            //var mySession = CreateSessionFactory().OpenSession();
            //var list = mySession.CreateCriteria(typeof(T)).List<T>();
            var list = CurrentSession.CreateCriteria(typeof(T)).List<T>();
            return list;
        }

        public IList<T> FindByQuery(string query)
        {
            //return CurrentSession.CreateQuery(query).List<T>();
            var xxx = CurrentSession.CreateSQLQuery(query);
            xxx.AddEntity("Fix", typeof(Fix));
            var list = xxx.List<T>();
            return list;
                
                
        }
   

        #endregion

        
    }
}